package kassiopeia.plugin.crawler.seed;

import java.net.MalformedURLException;
import java.net.URL;
import kassiopeia.plugin.DataProviderPlugin;
import kassiopeia.plugin.Event;
import kassiopeia.plugin.PluginException;
import org.apache.log4j.Logger;

public class UrlSeedPlugin extends DataProviderPlugin {

    private static final Logger logger = Logger.getLogger(UrlSeedPlugin.class);

    private static final String INITIAL_URL_KEY = "initial-urls";
    private static final String OUTPUT = "out";

    @Override
    public void beforeTask() {
        logger.info("UrlSeed plugin constructed : " + getContext().getPluginInstanceId());
    }

    @Override
    public void provideData() {
        String url = getContext().getParameter(INITIAL_URL_KEY);
        try {
            Event event = new Event(new URL(url));
            getContext().send(event, OUTPUT);
        } catch(MalformedURLException exception) {
            throw new PluginException("Probably invalid starting URL: " + url, exception);
        }
    }
}
